package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 管理老师
 * Created by Administrator on 2016/8/1 0001.
 */
@Controller
@RequestMapping(value = "page/manageTea")
public class ManageTeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private TeachingGrupServiceL teachingGrupServiceL;

    @Autowired
    private TeacherRankingLService teacherRankingLService;

    @Autowired
    private LogManageService logManageService;

    @Autowired
    private StudentGroupingService studentGroupingService;

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    @Autowired
    private CourseService courseService;


    /**
     * 根据老师id 或者 老师名字 或 老师职务 或老师身份证 查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/select")
    @ResponseBody public synchronized  Object manageSelectTea(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
        String manageId = request.getParameter("manageId");
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String teacherId = request.getParameter("teacherId");
        Teacher teacher = new Teacher();
        teacher.setTeacherposition("离职");
        teacher.setStartrow(startrow);
        teacher.setPageSize(pageSize);
        teacher.setTeacherid(teacherId);
        List<Teacher> teacherList = teacherService.selectByPaging(teacher);
        if (teacherList!=null&&teacherList.size()>0){
                TeacherData teacherData;
            TeacherStatistics teacherStatistics;
            TeacherRanking teacherRanking;
            for (int i = 0 ; i < teacherList.size() ; i ++) {
                map = new HashMap<String, Object>();
                teacher = teacherList.get(i);
                map.put("teacherId", teacher.getTeacherid());
                map.put("teacherCollege", teacher.getTeachercollege());
                map.put("teacherIdCard", teacher.getTeacheridcard());
                map.put("teacherName", teacher.getTeachername());
                map.put("teacherPhoneNumber", teacher.getTeacherphonenumber());
                map.put("teacherPosition", teacher.getTeacherposition());
                map.put("teacherSchool", teacher.getTeacherschool());
                map.put("teacherSex", teacher.getTeachersex());
                teacherData =teacherDataLService.selectByPrimaryKey(teacher.getTeacherid());
                map.put("myBirthd", teacherData.getMybirthd());
                map.put("myNickName", teacherData.getMynickname());
                map.put("teacherHeadPortrait", teacherData.getTeacherheadportrait());
                map.put("personalizedSignature", teacherData.getPersonalizedsignature());
                teacherStatistics=teacherService.selectByPrimaryKey(teacher.getTeacherid());
                if (teacherStatistics==null){
                    map.put("beViewed", 0);
                    map.put("beComments", 0);
                }else {
                    map.put("beViewed", teacherStatistics.getBeviewed());
                    map.put("beComments", teacherStatistics.getBecomments());
                }
//                teacherRanking = teacherRankingLService.selectByPrimaryKey(teacher.getTeacherid());
//                if (teacherRanking==null){
                    map.put("teacherRanking","暂无排名");
                    map.put("mySum","暂无分数");
//                }else {
//                    map.put("teacherRanking", teacherRanking.getTeacherranking());
//                    if (teacherRanking.getHeadcount()!=null&&teacherRanking.getHeadcount()>0){
//                        Float f = teacherRanking.getMysum() / teacherRanking.getHeadcount();
//                        map.put("mySum",f);
//                    }else {
//                        map.put("mySum",0);
//                    }
//                }

                mapList.add(map);
            }
            if (mapList!=null&&mapList.size()==teacherList.size()){
                objectMap.put("code",200);
                objectMap.put("message","成功");
                objectMap.put("data",mapList);
                 teacher = new Teacher();
                teacher.setTeacherposition("离职");
                teacher.setTeacherid(teacherId);
                objectMap.put("total",teacherService.countLimitNum(teacher));
//                Date date = new Date();
//                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                String logTime=time.format(date);
//                LogManage logManage = new LogManage();
//                logManage.setLogtime(logTime);
//                logManage.setManageid(manageId);
//                logManage.setLogcontent("查看教师列表");
//                logManageService.insertSelective(logManage);
            }else {
                objectMap.put("code",201);
                objectMap.put("message","失败");
            }
        }else {
            objectMap.put("code",202);
            objectMap.put("message","教师列表为空");
        }
        return objectMap;
    }

    /**
     * 修改老师信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/updTea")
    @ResponseBody synchronized  public Object  manageUpdTea(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teacherId = request.getParameter("teacherId");
        String teacherCollege = request.getParameter("teacherCollege");
        String teacherIdCard = request.getParameter("teacherIdCard");
        String teacherName = request.getParameter("teacherName");
        String teacherPhoneNumber = request.getParameter("teacherPhoneNumber");
        String teacherPosition = request.getParameter("teacherPosition");
        String teacherSchool = request.getParameter("teacherSchool");
        String teacherSex = request.getParameter("teacherSex");
        Teacher teacher = new Teacher();
        teacher.setTeacherid(teacherId);
        teacher.setTeachercollege(teacherCollege);
        teacher.setTeacheridcard(teacherIdCard);
        teacher.setTeachername(teacherName);
        teacher.setTeacherphonenumber(teacherPhoneNumber);
        teacher.setTeacherposition(teacherPosition);
        teacher.setTeacherschool(teacherSchool);
        teacher.setTeachersex(teacherSex);
        Teacher teacher1 = teacherService.selectByidRid(teacherId);
        List<Course> courseList =courseService.selectByTeacher(teacher1.getTeachername());
        if (teacherService.updateByPrimaryKeySelective(teacher)>0){
            TeacherData teacherData = new TeacherData();
            teacherData.setTeacherid(teacherId);
            teacherData.setTeachersex(teacherSex);
            teacherData.setTeachername(teacherName);
            teacherData.setTeacherphonenumber(teacherPhoneNumber);
            teacherDataLService.updateByPrimaryKeySelective(teacherData);
            if (teacherName!=""&&teacherName!=null){
                if (courseList!=null&&courseList.size()>0) {
                    for (int i = 0; i < courseList.size(); i++) {
                        Course course = new Course();
                        course.setCourseid(courseList.get(i).getCourseid());
                        course.setCourseteacher(teacherName);
                        courseService.updateByPrimaryKeySelective(course);
                    }
                }
            }
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("修改了一个老师信息:"+teacherId);
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code", 201);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }

    /**
     * 删除一个老师
     * @param request
     * @return
     */
    @RequestMapping(value = "/delOneTea")
    @ResponseBody public synchronized  Object manageDelOneTea(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teacherId = request.getParameter("teacherId");
//        String teacherName = request.getParameter("teacherName");//需要更改的
        String teacherPosition = "离职";
        Teacher teacher  = new Teacher();
        teacher.setTeacherid(teacherId);
        teacher.setTeacherposition(teacherPosition);
        if (teacherService.updateByPrimaryKeySelective(teacher)>0){
            String teacherName = teacherService.selectByPrimaryKeyo(teacherId).getTeachername();
            teacherService.deleteByPrimaryKey(teacherId);
            teacherDataLService.deleteByPrimaryKey(teacherId);
            teacherRankingLService.deleteByPrimaryKey(teacherId);
            teachingGrupServiceL.deleteByTeaId(teacherId);
            studentGroupingService.deleteByTeaId(teacherId);
            studentGroupingService.deleteByTeaIdG(teacherId);
            shiroUserRoleService.deleteByUserId(teacherId);
            List<Course> courseList =courseService.selectByTeacher(teacherName);
           if (courseList!=null&&courseList.size()>0){
               for (int i = 0 ; i < courseList.size() ; i++){
                   Course course = new Course();
                   course.setCourseid(courseList.get(i).getCourseid());
                   course.setCourseteacher("暂无老师上课");
                   courseService.updateByPrimaryKeySelective(course);
               }
           }
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("将一个老师设置为离职:"+teacherId);
            logManageService.insertSelective(logManage);
        }else {
            objectMap.put("code", 201);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }


    /**
     * 多个删除
     * @param request
     * @return
     */
    @RequestMapping(value = "/delTea")
    @ResponseBody public synchronized  Object  manageDelTea(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String [] teacherIds = request.getParameterValues("teacherId");
        String teacherPosition = "离职";
        if (teacherIds!=null&&teacherIds.length>0){
            String teacherId;
            for (int j = 0 ; j < teacherIds.length ; j ++){
                teacherId = teacherIds[j];
                Teacher teacher  = new Teacher();
                teacher.setTeacherid(teacherId);
                teacher.setTeacherposition(teacherPosition);
                if (teacherService.selectByPrimaryKeyo(teacherId)!=null&&teacherService.updateByPrimaryKeySelective(teacher)>0){
                    teacherService.deleteByPrimaryKey(teacherId);
                    teacherDataLService.deleteByPrimaryKey(teacherId);
                    teacherRankingLService.deleteByPrimaryKey(teacherId);
                    teachingGrupServiceL.deleteByTeaId(teacherId);
                    studentGroupingService.deleteByTeaId(teacherId);
                    studentGroupingService.deleteByTeaIdG(teacherId);
                    shiroUserRoleService.deleteByUserId(teacherId);
                    List<Course> courseList =courseService.selectByTeacher(teacherService.selectByPrimaryKeyo(teacherId).getTeachername());
                    if (courseList!=null&&courseList.size()>0){
                        for (int i = 0 ; i < courseList.size() ; i++){
                            Course course = new Course();
                            course.setCourseid(courseList.get(i).getCourseid());
                            course.setCourseteacher("暂无老师上课");
                            courseService.updateByPrimaryKeySelective(course);
                        }
                    }
                    objectMap.put("code", 200);
                    objectMap.put("message", "成功");
                    Date date = new Date();
                    SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String logTime=time.format(date);
                    LogManage logManage = new LogManage();
                    logManage.setLogtime(logTime);
                    logManage.setManageid(manageId);
                    logManage.setLogcontent("将一个老师设置为离职:"+teacherId);
                    logManageService.insertSelective(logManage);
                }else {
                    objectMap.put("code", 201);
                    objectMap.put("message", "失败");
                }
            }
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "数据不能传输为空");
        }
        return objectMap;
    }

    /**
     * 添加一个老师
     * @param request
     * @return
     */
    @RequestMapping(value = "/addTea")
    @ResponseBody public synchronized  Object manageAddTea(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String teacherId = request.getParameter("teacherId");
        String teacherCollege = request.getParameter("teacherCollege");
        String teacherIdCard = request.getParameter("teacherIdCard");
        String teacherName = request.getParameter("teacherName");
        String teacherPhoneNumber = request.getParameter("teacherPhoneNumber");
        String teacherPosition = request.getParameter("teacherPosition");
        String teacherSchool = request.getParameter("teacherSchool");
        String teacherSex = request.getParameter("teacherSex");
        Teacher teacher1 = teacherService.selectByidRid(teacherId);
        if (teacher1==null){
            Teacher teacher = new Teacher();
            teacher.setTeacherid(teacherId);
            teacher.setTeachercollege(teacherCollege);
            teacher.setTeacheridcard(teacherIdCard);
            teacher.setTeachername(teacherName);
            teacher.setTeacherphonenumber(teacherPhoneNumber);
            teacher.setTeacherposition(teacherPosition);
            teacher.setTeacherschool(teacherSchool);
            teacher.setTeachersex(teacherSex);
            TeacherData teacherData = new TeacherData();
            teacherData.setTeachername(teacherName);
            teacherData.setTeacherphonenumber(teacherPhoneNumber);
            teacherData.setTeacherid(teacherId);
            teacherData.setTeachersex(teacherSex);
            ShiroUserRole shiroUserRole = new ShiroUserRole();
            shiroUserRole.setUserId(teacherId);
            shiroUserRole.setRoleId(2);
            if (teacherDataLService.insertSelective(teacherData)>0&&teacherService.insertSelective(teacher)>0
                    &&shiroUserRoleService.insertSelective(shiroUserRole)>0){
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                Date date = new Date();
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String logTime=time.format(date);
                LogManage logManage = new LogManage();
                logManage.setLogtime(logTime);
                logManage.setManageid(manageId);
                logManage.setLogcontent("添加了一个老师信息:"+teacherId);
                logManageService.insertSelective(logManage);
            }else {
                objectMap.put("code", 201);
                objectMap.put("message", "失败");
            }
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "当前工号已存在");
        }
        return objectMap;
    }
}
